package com.guomao.utils;

import com.guomao.entity.*;
import com.guomao.functions.Collidable;
import com.guomao.functions.Hitable;
import com.guomao.functions.Recyclable;
import org.lwjgl.input.Keyboard;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class TankGameWindow extends Window {

    public TankGameWindow(String title, int width, int height, int fps) {
        super(title, width, height, fps);
    }

    Tank tank;
    Element e;
    EnemyTank et;
    long eatBuffTime;
    Buff buff;
    Boss boss;
    boolean flag = false;
//    BrickWall bw;
//    GrassWall gw;
//    ArrayList<BrickWall> bws = new ArrayList<BrickWall>();
//    ArrayList<GrassWall> gws = new ArrayList<GrassWall>();

    //    ArrayList集合不支持在增强for或者迭代器遍历时修改集合结构
//    List<Element> es = new ArrayList<>();
    List<Element> es = new CopyOnWriteArrayList<>();

    //创建窗体时执行且只执行一次
    @Override
    protected void onCreate() {
        tank = new Tank(512, 512);
        et = new EnemyTank(512, 0);
        buff = new Buff(512 + 256, 512);
        boss = new Boss(64 * 11, 64 * 11);
        es.add(buff);
        es.add(tank);
        es.add(et);
        es.add(boss);

//循环的次数 屏幕的宽度/64
        for (int i = 0; i < Meta.WIDTH / 64; i++) {
            if ((i >= 3 && i <= 5) || (i >= 12 && i <= 16)) {
                e = new GrassWall((i * 64), 64);
                es.add(e);
            } else {
                e = new BrickWall((i * 64), 64);
                es.add(e);
            }
        }

        for (int i = 0; i < Meta.WIDTH / 64 - 3; i++) {
            if (i >= 8 && i <= 13) {
                e = new GrassWall((i * 64) + 192, 192);
                es.add(e);
            } else {
                e = new SteelWall((i * 64) + 192, 192);
                es.add(e);
            }
        }

        for (int i = 0; i < Meta.WIDTH / 64 - 7; i++) {
            if (i % 2 == 0) {
                e = new BrickWall((i * 64) + 384, 320);
                es.add(e);
            } else {
                e = new SteelWall((i * 64) + 384, 320);
                es.add(e);
            }
        }

        for (int i = 0; i < Meta.WIDTH / 64 - 11; i++) {
            e = new BrickWall((i * 64) + 512, 448);
            es.add(e);
        }

        for (int i = 0; i < Meta.WIDTH / 64 - 13; i++) {
            e = new SteelWall((i * 64) + 512 + 192, 448 + 128);
            es.add(e);
        }

        for (int i = 0; i < Meta.WIDTH / 64 - 19; i++) {
            e = new BrickWall((i * 64) + 640, 640);
            es.add(e);
        }

        for (int i = 0; i < 3; i++) {
            if (i == 1) continue;
            e = new BrickWall((i * 64) + 640, 640 + 64);
            es.add(e);
        }

        for (int i = 0; i < Meta.HEIGHT / 64 - 2; i++) {
            if (i == 8) continue;
            e = new WaterWall(64, (i * 64) + 128);
            es.add(e);
        }

        for (int i = 0; i < Meta.HEIGHT / 64 - 5; i++) {
            if (i == 8) continue;
            e = new BrickWall(256, (i * 64) + 256);
            es.add(e);
        }

        for (int i = 0; i < Meta.HEIGHT / 64 - 7; i++) {
            e = new WaterWall(448 - 64, (i * 64) + 448);
            es.add(e);
        }

        for (int i = 0; i < 2; i++) {
            e = new WaterWall(1024, (i * 64) + 640);
            es.add(e);
        }

        for (int i = 0; i < Meta.HEIGHT / 64 - 6; i++) {
            if (i == Meta.HEIGHT / 64 - 6 - 1) break;
            e = new BrickWall(1280, (i * 64) + 384);
            es.add(e);
        }


//        bw = new BrickWall(512, 512);
//        System.out.println("onCreate 执行.....");

       /* try {
            SoundUtils.play("day15-javase-tank//audio//start.wav", false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
*/

    }

    @Override
    protected void onMouseEvent(int key, int x, int y) {
        System.out.println(key + ":" + x + ":" + y);
    }

    /**
     * 键盘事件 按下按键触发
     *
     * @param key 按键，{@code Keyboard.key_xx}
     */
    @Override
    protected void onKeyEvent(int key) {
//        System.out.println(key);
//        if (key == Keyboard.KEY_SPACE) {
//            System.out.println("空白格");
//        }
        switch (key) {
            case Keyboard.KEY_UP:
                tank.move(Direction.UP);
                break;
            case Keyboard.KEY_DOWN:
                tank.move(Direction.DOWN);
                break;
            case Keyboard.KEY_LEFT:
                tank.move(Direction.LEFT);
                break;
            case Keyboard.KEY_RIGHT:
                tank.move(Direction.RIGHT);
                break;
            case Keyboard.KEY_SPACE:
                Bullet bullet = tank.fire();
                if (bullet != null) {
                    es.add(bullet);
                }
                break;
/*            case Keyboard.KEY_W:
                et.move(Direction.UP);
                break;
            case Keyboard.KEY_S:
                et.move(Direction.DOWN);
                break;
            case Keyboard.KEY_A:
                et.move(Direction.LEFT);
                break;
            case Keyboard.KEY_D:
                et.move(Direction.RIGHT);
                break;*/
        }
    }

    //每秒钟调用fps次 绘制图片
    @Override
    protected void onDisplayUpdate() {

        if (flag) {
            try {
                DrawUtils.draw("day16-javase-tank/images/over.png", 300, 200);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        for (Element e : es) {
            e.draw();
            if (e instanceof Recyclable) {
                Recyclable r = (Recyclable) e;
                if (r.isOutside()) es.remove(e);
            }
            if (e instanceof EnemyTank) {
                EnemyTank etk = (EnemyTank) e;
                Bullet bullet = etk.fire();
                if (bullet != null) {
                    es.add(bullet);
                }
            }

            if (e instanceof Recyclable && e instanceof Boss) {
                Boss boss = (Boss) e;
                if (boss.isOutside()) {
                    es.clear();
                    flag = true;
                }
            }
        }

        for (Element e1 : es) {
            if (e1 instanceof Tank) {
                Tank tank = (Tank) e1;
                for (Element e2 : es) {
                    if (e2 instanceof Collidable) {
                        boolean flag = tank.isCollide(e2);
                        if (flag) break;
                    }
                }
            }
        }

        for (Element e1 : es) {
            if (e1 instanceof EnemyTank) {
                EnemyTank etank = (EnemyTank) e1;
                for (Element e2 : es) {
                    if (e2 instanceof Collidable) {
                        boolean flag = etank.isCollide(e2);
                        if (flag) break;
                    }
                }
            }
        }

        for (Element e1 : es) {
            if (e1 instanceof Bullet) {
                Bullet bullet = (Bullet) e1;
                for (Element e2 : es) {
                    if (e2 instanceof Hitable) {
                        boolean flag = bullet.isCollide(e2);
                        //如果flag为true说明墙和子弹发生了碰撞
                        if (flag) {
                            es.remove(e1);//移除的是子弹
                            Hitable e3 = (Hitable) e2;
                            Blast blast = e3.showBlast();
                            if (blast != null) {
                                es.add(blast);
                            }
                            break;
                        }
                    }
                }
            }
        }

        for (Element e1 : es) {
            if (e1 instanceof Buff) {
                Buff buff = (Buff) e1;
                for (Element e2 : es) {
                    if (e2 instanceof Tank) {
                        Tank tank = (Tank) e2;
                        if (buff.isCover(e2)) {
                            es.remove(e1);
                            tank.distance = 64;
                            eatBuffTime = System.currentTimeMillis();
                        }
                    }
                }
            }
        }

        for (Element e : es) {
            if (e instanceof Tank) {
                Tank t = (Tank) e;
                long now = System.currentTimeMillis();
                //如果buff持续时间超过了5秒钟 重新设定速度为默认值16
                if (now - eatBuffTime >= 5000) t.distance = 16;
            }
        }


       /* for (Element e1 : es) {
            if (e1 instanceof Tank) {
                Tank tank = (Tank) e1;
//                System.out.println("aaaa");
                for (Element e2 : es) {
//                    System.out.println("bbbb");
                    if (e2 instanceof BrickWall) {
                        BrickWall brickWall = (BrickWall) e2;
                        boolean flag = tank.isCollide(brickWall);
                        if (flag) {
                            System.out.println("检测到碰撞!!!!");
                            break;
                        }
                    }

                    if (e2 instanceof SteelWall) {
                        SteelWall steelWall = (SteelWall) e2;
                        boolean flag = tank.isCollide(steelWall);
                        if (flag) {
                            System.out.println("检测到碰撞!!!!");
                            break;
                        }
                    }

                    if (e2 instanceof WaterWall) {
                        WaterWall waterWall = (WaterWall) e2;
                        boolean flag = tank.isCollide(waterWall);
                        if (flag) {
                            System.out.println("检测到碰撞!!!!");
                            break;
                        }
                    }
                }
            }
        }*/
    }
}
